//
//  MyConcernConfigListViewController.m
//  homeland
//
//  Created by SunShine on 13-10-21.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "MyConcernConfigListViewController.h"
#import "StreetInfoItem.h"
#import "UnitInfoItem.h"
#import "MyConcernDataService.h"

@interface MyConcernConfigListViewController ()
@property (strong, nonatomic) MyConcernConfigService *dataService;
@end

@implementation MyConcernConfigListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"信息定制";
    self.dataService = [MyConcernConfigService getService];
    //init segment
//    _segmentController = [[HMSegmentedControl alloc]initWithSectionTitles:[NSArray arrayWithObjects:@"按街道筛选", @"按部门筛选", @"按类别筛选", nil]];
//    _segmentController.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10);
//    _segmentController.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
//    _segmentController.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
//    _segmentController.scrollEnabled = YES;
//    [_segmentController setFrame:CGRectMake(0, 0, 320, 44)];
//    [_segmentController addTarget:self action:@selector(segmentValueChangedAction:) forControlEvents:UIControlEventValueChanged];
//    [self.view addSubview:_segmentController];
//    [self startLoadingView];
    //发起请求
    [_dataService loadDataWithKey:nil];
    [_tableView reloadData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //调整界面
//    if (_dataService.currentConfigType == MyConcernConfigTypeFile) {
//        _segmentController.hidden = YES;
//        CGRect tableRect = _tableView.frame;
//        tableRect.origin.y = tableRect.origin.y - CGRectGetHeight(_segmentController.frame);
//        tableRect.size.height = tableRect.size.height + CGRectGetHeight(_segmentController.frame);
//        _tableView.frame = tableRect;
//    } else {
//        [self segmentValueChangedAction:nil];
//    }
    _dataService.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)isArray:(NSArray *)array ContainsKey:(NSString *)key
{
    for (NSString *item in array) {
        if ([item isEqualToString:key]) {
            return YES;
        }
    }
    return NO;
}

#pragma mark - Table Delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataService.dataList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"MyConcernConfigCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    //显示标题
    NSString *title;
    NSString *key;
    id item = [_dataService.dataList objectAtIndex:indexPath.row];
    if ([item isKindOfClass:[NSString class]]) {
        title = [_dataService.dataList objectAtIndex:indexPath.row];
        key = title;
    } else if ([item isKindOfClass:[StreetInfoItem class]]) {
        title = ((StreetInfoItem *)item).item_name;
        key = [NSString stringWithFormat:@"%d",((StreetInfoItem *)item).item_id.integerValue];
    } else if ([item isKindOfClass:[UnitInfoItem class]]) {
        title = ((UnitInfoItem *)item).item_name;
        key = [NSString stringWithFormat:@"%d",((UnitInfoItem *)item).item_id.integerValue];
    }
    cell.textLabel.text = title;
    if ([self isArray:[_dataService getCurrentSelectList] ContainsKey:key]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *key;
    id item = [_dataService.dataList objectAtIndex:indexPath.row];
    if ([item isKindOfClass:[NSString class]]) {
        key = (NSString *)item;
    } else if ([item isKindOfClass:[StreetInfoItem class]]) {
        key = [NSString stringWithFormat:@"%d",((StreetInfoItem *)item).item_id.integerValue];
    } else if ([item isKindOfClass:[UnitInfoItem class]]) {
        key = [NSString stringWithFormat:@"%d",((UnitInfoItem *)item).item_id.integerValue];
    }
    if ([self isArray:[_dataService getCurrentSelectList] ContainsKey:key]) {
        //已有
        [[_dataService getCurrentSelectList] removeObject:key];
    } else {
        //没有
        [[_dataService getCurrentSelectList] addObject:key];
    }
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

//#pragma mark - Segment Action
//- (IBAction)segmentValueChangedAction:(id)sender
//{
//    [_dataService selectSegmentBarAtIndex:_segmentController.selectedSegmentIndex];
//    [self startLoadingView];
//    //发起请求
//    [_dataService loadDataWithKey:nil];
//    [_tableView reloadData];
//}

- (IBAction)saveButtonAction:(id)sender
{
    [self startLoadingViewWithText:@"正在保存..."];
    MyConcernDataService *dataService = [[MyConcernDataService alloc]init];
    dataService.delegate = self;
    [dataService saveMyConcernConfigs];
}

#pragma mark - Data Service Delegate
-(void)loadDataFinished
{
    [self stopLoadingView];
    [_tableView reloadData];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"操作成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
}

-(void)setEnableToLoadMore:(BOOL)enable
{
    
}
@end
